package edu.office.applicationRepository;


import edu.office.entities.Predicate;
import edu.office.tools.EOMessage;
import edu.office.tools.EOMessage1;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;

import java.util.LinkedList;
import java.util.List;


@Named("predicateRepositoryA")
@ApplicationScoped
public class PredicateRepository extends ApplicationCommonRepository<Predicate> {
    List<Predicate> allPredicate = new LinkedList<>();
    Predicate predecessor;

    public List<Predicate> getAllPredicate() {
        if (allPredicate.isEmpty() || isDataChanged()) {
            allPredicate.clear();
            allPredicate = listAll();
        }
        return allPredicate;
    }

    public Predicate getPredecessor() {
        if (null == predecessor) {
            for (Predicate predicate : getAllPredicate()) {
                if (predicate.getPNAME().equals("Predecessor")) {
                    predecessor = predicate;
                    break;
                }
            }
        }
        return predecessor ;
    }

    public String mycreate(Predicate predicate) {
        if (list("name", predicate.getPNAME()).isEmpty()) {
            create(predicate);
            return EOMessage.getEOMessageENUM(EOMessage1.Succeed);
        } else {
            return EOMessage.getEOMessageENUM(EOMessage1.Already_Exist);
        }

    }

    public List<Predicate> getPredicateList(String searchName) {
        String conditonString = "where  locate ('" + searchName.toLowerCase() + "',LOWER(pname))>0 or  locate ('" + searchName.toLowerCase() + "',LOWER(meaning))>0";
        return list("select * from Predicate " + conditonString + " order by pname");
    }

}
